package com.yuanfeng.order.controller;


import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.order.ShoppingCartVO;
import io.seata.core.exception.TransactionException;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName OrderUserCartController
 * @Description 购物车表
 * @Author cyp
 * @Date 2023-05-25 13:39:40
 * @Version 1.0
 **/
@RestController
@RequestMapping("order/userCart")
public class OrderUserCartController extends BaseController{

    /**
     * 查询商品在购物车的数量
     */
    @PostMapping("/selectCount")
    public Integer selectCurrentCartQuantity(@RequestParam("userId")String userId,@RequestParam("goodsId")Integer goodsId){
        Integer integer = orderUserCartService.selectCurrentCartQuantity(userId, goodsId);
        return integer;
    }
    /**
     * 购物车  修改商品数量
     */
    @PostMapping("/changeGoodsNum")
    public ResponseResult changeGoodsNum(@RequestBody Map<String, Object> map) {
        ResponseResult result = orderUserCartService.changeGoodsNum(map);
        return result;
    }

    /**
     * 购物车  批量删除
     */
    @PostMapping("/batchDeleteGoods")
    public ResponseResult batchDeleteGoods(@RequestBody Map<String, Object> map) {
        ResponseResult result = orderUserCartService.batchDeleteGoods(map);
        return result;
    }


    /**
     * 查询购物车商品并分组
     *
     * @param map
     * @return
     */
    @PostMapping("/queryShoppingCart")
    public ResponseResult queryShoppingCart(@RequestBody Map<String, Object> map) {
        ResponseResult vo=getUserInfoByToken(map.get("token").toString());
        if(vo.getCode()== CommonType.commonStatus.YES.getCode()){
            ResponseResult result = orderUserCartService.queryShoppingCart(map);
            return result;
        }
        return vo;
    }

    /**
     * 删除购物车无效商品并分组
     *
     * @param map
     * @return
     */
    @PostMapping("/deleteInvalidProductByCart")
    public ResponseResult deleteInvalidProductByCart(@RequestBody Map<String, Object> map) {
        ResponseResult vo=getUserInfoByToken(map.get("token").toString());
        if(vo.getCode()== CommonType.commonStatus.YES.getCode()){
            ResponseResult result = orderUserCartService.deleteInvalidProductByCart(map);
            return result;
        }
        return vo;
    }

    /**
     * 插入购物车
     *
     * @param map
     * @return
     */
    @PostMapping("/insertShoppingCart")
    public ResponseResult insertShoppingCart(@RequestBody Map<String, Object> map) throws TransactionException {

        ResponseResult vo=getUserInfoByToken(map.get("token").toString());
        if(vo.getCode()== CommonType.commonStatus.YES.getCode()){
            ResponseResult result = orderUserCartService.insertShoppingCart(map);
            return result;
        }
        return vo;
    }

    /**
     * 热门推荐商品 插入购物车
     *
     * @param map
     * @return
     */
    @PostMapping("/business/insertShoppingCartBatch")
    public ResponseResult insertShoppingCartBatch(@RequestBody Map<String, Object> map){
        ResponseResult result = orderUserCartService.insertShoppingCartBatch(map);
        return result;
    }


    /**
     *平台查看用户购物车
     * @param param
     * @return
     */
    @PostMapping("/platform/list/shoppingCart")
    public ResponseResult getShoppongCartList(@RequestBody(required = false) ShoppingCartVO param) throws IllegalAccessException {
        ResponseResult result = orderUserCartService.getShoppongCartList(param);
        return result;
    }

    /**
     * 更新购物车 除shopId无其他条件
     *
     * @param map
     * @return
     */
    @PostMapping("/updateOrderUserCar")
    public ResponseResult updateOrderUserCarByMap(@RequestBody Map<String, Object> map) {
        ResponseResult result = orderUserCartService.updateOrderUserCarByMap(map);
        return result;
    }


    /**
     * 删除商品数量
     *
     * @param map
     * @return
     */
    @PostMapping("/h5/reduceGoodsNum")
    public ResponseResult reduceGoodsNum(@RequestBody Map<String, Object> map) {
        ResponseResult result = orderUserCartService.reduceGoodsNum(map);
        return result;
    }

    /**
     * 添加商品数量
     *
     * @param map
     * @return
     */
    @PostMapping("/h5/addGoodsNum")
    public ResponseResult addGoodsNum(@RequestBody Map<String, Object> map) {
        ResponseResult result = orderUserCartService.addGoodsNum(map);
        return result;

    }
}

